package com.atguigu.view;

import com.atguigu.domain.Employee;
import com.atguigu.domain.Programmer;
import com.atguigu.service.NameListService;
import com.atguigu.service.TeamException;
import com.atguigu.service.TeamService;

/**
 * @author : leimingchao
 * @date Date : 2021年09月16日 0:13
 */
public class TeamView {

    private NameListService listSvc = new NameListService();

    private TeamService teamSvc = new TeamService();

    public void enterMainMenu() {

        boolean  loopFlag = true;
        char menu = 0;
        while (loopFlag) {
            if (menu != '1') {
                listAllEmployee();
            }
            System.out.println("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):");
            menu = TSUtility.readMenuSelection();
            switch (menu) {
                case '1':
                    getTeam();
                    break;
                case '2':
                    addMember();
                    break;
                case '3':
                    deleteMember();
                    break;
                case '4':
                    System.out.println("是否退出(Y/N):");
                    char isExist = TSUtility.readConfirmSelection();
                    if (isExist == 'Y') {
                        loopFlag = false;
                    }
                    break;
            }
        }
    }

    private void listAllEmployee() {
        // System.out.println("显示公司所有的员工信息");
        System.out.println("-----------------------------开发团队调度软件-----------------------------\n");
        Employee[] employees = listSvc.getAllEmployees();
        if (employees == null || employees.length == 0) {
            System.out.println("公司中没有任何员工的信息！");
        } else {
            System.out.println("Id\t\t姓名\t\t\t年龄\t\t工资\t\t职位\t\t状态\t\t奖金\t\t股票\t\t领用设备");
            int length = employees.length;
            for (int i = 0; i < length; i++) {
                System.out.println(employees[i]);
            }
        }
        System.out.println("----------------------------------------------------------------------------");
    }

    private void getTeam() {

        // System.out.println("查看开发团队情况");
        System.out.println("----------------------------------团队成员列表--------------------------------------------\n");

        Programmer[] team = teamSvc.getTeam();
        if (team == null || team.length == 0) {
            System.out.println("开发团队目前没有成员！");
        } else {
            System.out.println("TID/ID\t\t姓名\t\t年龄\t\t工资\t\t职位\t\t奖金\t\t股票\n");
            int length = team.length;
            for (int i = 0; i < length; i++) {
                System.out.println(team[i].getDetailsForTeam());
            }
        }
        System.out.println("------------------------------------------------------------------------------------------");
    }


    private void addMember() {
        // System.out.println("添加团队成员");
        System.out.println("----------------------------------添加成员--------------------------------------------");
        System.out.print("请输入要添加的员工ID:");
        int id = TSUtility.readInt();
        try {
            Employee employee = listSvc.getEmployee(id);
            teamSvc.addMember(employee);
            System.out.println("添加成功");
        } catch (TeamException e) {
            System.out.println("添加失败，原因：" + e.getMessage());
        }
        TSUtility.readReturn();
    }

    private void deleteMember() {
        // System.out.println("删除团队成员");
        System.out.println("----------------------------------删除成员--------------------------------------------");
        System.out.println("请输入要删除员工的TID:");
        int memberId = TSUtility.readInt();
        System.out.print("确认是否删除(Y/N):");
        char isDelete = TSUtility.readConfirmSelection();
        if (isDelete == 'N') {
            return;
        }

        try {
            teamSvc.removeMember(memberId);
            System.out.println("删除成功");
        } catch (TeamException e) {
            System.out.println("删除失败，原因：" + e.getMessage());
        }
        TSUtility.readReturn();

    }

    public static void main(String[] args) {

        TeamView teamView = new TeamView();

        teamView.enterMainMenu();
    }

}
